package movEat.GUI;

import movEat.Definitions.actorDB;
import movEat.IKernel.IKernel;
import movEat.IKernel.Operation;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;

public class FindActorForMainCourse {

	ActorParameters findactor;
	IKernel ker = null;
	Combo actorsFoundCombo ;
	Button select; 
	final Button find;

	Label selectedLabel;
	private Button back;
	public FindActorForMainCourse(final IKernel ker, final Label label){

		this.ker = ker;
		selectedLabel = label;

		GUI.subShell = SWTUtil.getShell(SWT.TITLE);
		GUI.subShell.setLayout(new GridLayout(2,true));
		GUI.subShell.setBackgroundImage(GUI.mainBackground);
		GUI.subShell.setBackgroundMode(SWT.INHERIT_DEFAULT);
		GUI.subShell.setText("Select your favourite spice");

		findactor = new ActorParameters(GUI.subShell);

		find = new Button(GUI.subShell, SWT.PUSH);
		find.setText("Find!");
		find.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 10, SWT.BOLD));
		find.setLayoutData(new GridData(SWT.FILL, SWT.NONE, false, false, SWT.FILL, SWT.NONE));

		find.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e){

				actorDB actor = findactor.getActorDBFromUserInput_find(GUI.subShell);
				if(actor!=null){

					find.setEnabled(false);
					select.setEnabled(false);
					actorsFoundCombo.setEnabled(false);
					back.setEnabled(false);

					ker.ButtonPushed(Operation.FIND_ACTOR_FOR_MAIN_COURSE);
					return;
				}

				find.setEnabled(true);
				back.setEnabled(true);
			}
		});


		findactor.addEnableDisableToRelatedButton(find);

		actorsFoundCombo = new Combo(GUI.subShell, SWT.DROP_DOWN);
		actorsFoundCombo.setLayoutData(new GridData(SWT.FILL, SWT.NONE, false, false, SWT.FILL, SWT.NONE));
		actorsFoundCombo.setText("Select an Actor");
		actorsFoundCombo.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 10, SWT.BOLD));
		actorsFoundCombo.setEnabled(false);

		select = new Button(GUI.subShell, SWT.PUSH);
		select.setLayoutData(new GridData(SWT.FILL, SWT.NONE, false, false));
		select.setText("Select!");
		select.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 10, SWT.BOLD));
		select.setEnabled(false);
		select.addSelectionListener(new SelectionAdapter() {

			public void widgetSelected(SelectionEvent e){

				if(actorsFoundCombo.getText().equals("Select An Actor")){
					GUI.userMessage(GUI.subShell,"Error", "Please select An Actor");
					return;
				}
				label.setText(actorsFoundCombo.getText());
				
				GUI.subShell.close();
			}
		});

		back = new Button(GUI.subShell, SWT.PUSH);
		back.setLayoutData(new GridData(SWT.FILL, SWT.NONE, false, false));
		back.setText("Back");
		back.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 10, SWT.BOLD));
		back.addSelectionListener(new SelectionAdapter() {

			public void widgetSelected(SelectionEvent e){

				GUI.subShell.close();

			}
		});

		GUI.subShell.pack();

		GUI.subShell.addListener(SWT.Close, new Listener(){ 

			@Override
			public void handleEvent(Event event) 
			{ 
				event.doit=true;
				
				GUI.shell.setEnabled(true);
				GUI.shell.forceActive();
				
				GUI.subShell.dispose();
			}
		});

		GUI.shell.setEnabled(false);
		GUI.subShell.open();
	}

	public void setActorsFound(final String[] actorsFound){
		SWTUtil.getDisplay().syncExec(new Runnable() {

			@Override
			public void run() {


				find.setEnabled(true);
				back.setEnabled(true);

				if( actorsFound==null || actorsFound.length==0 ){
					String error = "Sorry, but the search could'nt find any results.";
					GUI.userMessage(GUI.subShell,"No Results", error);
					return;
				}

				actorsFoundCombo.setItems(actorsFound);
				actorsFoundCombo.setEnabled(true);
				actorsFoundCombo.setText("Select An Actor");
				select.setEnabled(true);
				back.setEnabled(true);


			}
		});


	}

	public void getActorParams(actorDB actor) {
		actor.setFirstName(findactor.getFirstName());
		actor.setLastName(findactor.getLastName());
	}
}